/*
 * This file is part of "IdeWiki" project.
 *
 * Copyright (c) 2008 Hendrik Ebel
 * All Rights Reserved.
 *
 * Please visit http://code.google.com/p/idewiki for updates and contact.
 *
 * --LICENSE NOTICE--
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 3
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; If not, see <http://www.gnu.org/licenses/>.
 * --LICENSE NOTICE--
 */
package ws.ebel.idewiki.util;

import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.graphics.Image;
import ws.ebel.idewiki.util.URLEncoderDecoder;

import ws.ebel.idewiki.Activator;
import ws.ebel.idewiki.IdeWikiConstants;

/**
 * The Class CommonHelper.
 */
public class CommonHelper {

    //  TODO: add space char to prefs
    /** The ENCODE d_ SPACE. */
    public static String ENCODED_SPACE = "+";

    /** The ENCODING. */
    private static String ENCODING = "UTF-8";

    /**
     * Creates the image.
     * 
     * @param path the path
     * 
     * @return the image
     */
    public static Image createImage(String path) {
        Image result = null;
        URL installURL = Activator.getDefault().getBundle().getEntry("/");
        ImageDescriptor imageDesc;
        try {
            imageDesc = ImageDescriptor.createFromURL(new URL(installURL, path));
            result = imageDesc.createImage();
        } catch (MalformedURLException e1) {
            Activator.getDefault().reportError("MalformedURLException", e1.getMessage());
        }
        return result;
    }

    /**
     * Gets the start url.
     * 
     * @param quickLoginEnabled the quick login enabled
     * 
     * @return the start url
     */
    public static String getStartUrl(boolean quickLoginEnabled) {
        // http://localhost:8668/exec/quicklogin?login=user&password=****
        if (Activator.getDefault().getIsAutoLoginEnabled() || quickLoginEnabled) {
            StringBuffer startUrl = new StringBuffer();
            startUrl.append(Activator.getDefault().getWikiUrlPreference());
            startUrl.append(IdeWikiConstants.QUICK_LOGIN_USER_PREFIX);
            startUrl.append(Activator.getDefault().getUserNamePreference());
            startUrl.append(IdeWikiConstants.QUICK_LOGIN_PASS_PREFIX);
            startUrl.append(Activator.getDefault().getUserPasswordPreference());
            return startUrl.toString();
        }
        // http://localhost:8668/space/start
        else {
            return Activator.getDefault().getWikiUrlPreference() + IdeWikiConstants.START_URL_IDENTIFIER;
        }
    }

    /**
     * Gets the start url.
     * 
     * @return the start url
     */
    public static String getStartUrl() {
        return getStartUrl(false);
    }

    /**
     * Checks if is java class name part.
     * 
     * @param c the c
     * 
     * @return true, if is java class name part
     */
    public static boolean isJavaClassNamePart(char c) {
        return Character.isJavaIdentifierPart(c) || c == '.';
    }

    /**
     * Gets the project name.
     * 
     * @param location like http://127.0.0.1:8668/space/IdeMacro?p=MyProject&c=org.snipsnap.net.SnipLockServlet
     * 
     * @return project name like MyProject
     */
    public static String getProjectName(String location) {
        String result = null;
        if (location != null && location.contains("&c=") && location.contains("p=")) {
            result = location.substring(location.indexOf("p=") + 2, location.indexOf("&c="));
        }

        return result;
    }

    /**
     * Gets the resource name.
     * 
     * @param location like http://127.0.0.1:8668/space/IdeMacro?p=MyProject&c=org.snipsnap.net.SnipLockServlet
     * 
     * @return resource like org.snipsnap.net.SnipLockServlet
     */
    public static String getResourceName(String location) {
        String result = "Unknown resource name!";
        if (location != null && location.contains("&c=") && location.contains("p=")) {
            result = location.substring(location.indexOf("&c=") + 3, location.length());
        }

        return result;
    }

    /**
     * Encode.
     * 
     * @param aStr the string
     * 
     * @return the string
     */
    public static String encode(String aStr) {
        if (aStr != null) {
            try {

                if (null != ENCODED_SPACE && ENCODED_SPACE.length() > 0) {
                    return URLEncoderDecoder.encode(aStr, ENCODING).replace(' ', ENCODED_SPACE.charAt(0));
                } else {
                    return URLEncoderDecoder.encode(aStr, ENCODING);
                }
            } catch (UnsupportedEncodingException e) {
                Activator.getDefault().logAndReport("Encoding Error", "Encoding will be not supported!", e);
                return aStr;
            }
        } else {
            return aStr;
        }
    }

    /**
     * Decode.
     * 
     * @param aStr the string
     * 
     * @return the string
     */
    public static String decode(String aStr) {
        if (aStr != null) {

            try {
                if (null != ENCODED_SPACE && ENCODED_SPACE.length() > 0) {
                    aStr = aStr.replace(ENCODED_SPACE.charAt(0), ' ');
                }
                return URLEncoderDecoder.decode(aStr, ENCODING);
            } catch (UnsupportedEncodingException e) {
                Activator.getDefault().logAndReport("Encoding Error", "Encoding will be not supported!", e);
                return aStr;
            }
        } else {
            return aStr;
        }
    }

}
